<template>
	<div class="sys-role-container">
		<el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
			<el-form :model="state.queryParams" ref="queryForm" :inline="true">
				<el-form-item :label="$t('system.role.roleName')">
					<el-input v-model="state.queryParams.name" :placeholder="$t('system.role.roleName')" clearable />
				</el-form-item>
				<el-form-item :label="$t('system.role.roleCode')">
					<el-input v-model="state.queryParams.code" :placeholder="$t('system.role.roleCode')" clearable />
				</el-form-item>
				<el-form-item>
					<el-button-group>
						<el-button type="primary" icon="ele-Search" @click="handleQuery" v-auth="'sysRole:page'"> {{ $t('system.btn.query') }} </el-button>
						<el-button icon="ele-Refresh" @click="resetQuery"> {{ $t('system.btn.reset') }} </el-button>
					</el-button-group>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" icon="ele-Plus" @click="openAddRole" v-auth="'sysRole:addRole'"> {{ $t('system.btn.add') }} </el-button>
				</el-form-item>
			</el-form>
		</el-card>

		<el-card class="full-table" shadow="hover" style="margin-top: 5px">
			<el-table :data="state.roleData" style="width: 100%" v-loading="state.loading" border>
				<el-table-column type="index" :label="$t('system.common.index')" width="55" align="center" fixed />
				<el-table-column prop="name" :label="$t('system.role.roleName')" align="center" show-overflow-tooltip />
				<el-table-column prop="code" :label="$t('system.role.roleCode')" align="center" show-overflow-tooltip />
				<el-table-column :label="$t('system.role.dataScope')" align="center" show-overflow-tooltip>
					<template #default="scope">
            <g-sys-dict v-model="scope.row.dataScope" code="DataScopeEnum" />
					</template>
				</el-table-column>
				<el-table-column prop="sort" :label="$t('system.common.sort')" width="70" align="center" show-overflow-tooltip />
				<el-table-column :label="$t('system.common.status')" width="70" align="center" show-overflow-tooltip>
					<template #default="scope">
						<el-switch v-model="scope.row.isDisable" :active-value="1" :inactive-value="0" size="small" @change="changeStatus(scope.row)" v-auth="'sysRole:setStatus'" />
					</template>
				</el-table-column>
				<el-table-column :label="$t('system.common.modifyRecord')" width="100" align="center" show-overflow-tooltip>
					<template #default="scope">
						<ModifyRecord :data="scope.row" />
					</template>
				</el-table-column>
				<el-table-column :label="$t('system.common.operation')" width="320" fixed="right" align="center" show-overflow-tooltip>
					<template #default="scope">
						<el-button icon="ele-Edit" size="small" text type="primary" @click="openEditRole(scope.row)" v-auth="'sysRole:updateRole'"> {{ $t('system.btn.edit') }} </el-button>
						<el-button icon="ele-Menu" size="small" text type="primary" @click="openGrantMenu(scope.row)" v-auth="'sysRole:grantMenu'"> {{ $t('system.btn.grantMenu') }} </el-button>
						<el-button icon="ele-OfficeBuilding" size="small" text type="primary" @click="openGrantData(scope.row)" v-auth="'sysRole:grantDataScope'"> {{ $t('system.btn.grantData') }} </el-button>
						<el-button icon="ele-Delete" size="small" text type="danger" @click="delRole(scope.row)" v-auth="'sysRole:deleteRole'"> {{ $t('system.btn.delete') }} </el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				v-model:currentPage="state.tableParams.pageNo"
				v-model:page-size="state.tableParams.pageSize"
				:total="state.tableParams.total"
				:page-sizes="[10, 20, 50, 100]"
				size="small"
				background
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				layout="total, sizes, prev, pager, next, jumper"
			/>
		</el-card>

		<EditRole ref="editRoleRef" :title="state.editRoleTitle" @handleQuery="handleQuery" />
		<GrantData ref="grantDataRef" @handleQuery="handleQuery" />
		<GrantMenu ref="grantMenuRef" :title="state.editRoleTitle" @handleQuery="handleQuery" />
	</div>
</template>

<script lang="ts" setup name="sysRole">
import { onMounted, reactive, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import EditRole from '/@/views/system/role/component/editRole.vue';
import GrantData from '/@/views/system/role/component/grantData.vue';
import GrantMenu from '/@/views/system/role/component/grantMenu.vue';
import ModifyRecord from '/@/components/table/modifyRecord.vue';

import { apiSysRoleDeletePost, apiSysRolePagePost,apiSysRoleSetStatusPost } from '/@/api-services/api';
 
const editRoleRef = ref<InstanceType<typeof EditRole>>();
const grantDataRef = ref<InstanceType<typeof GrantData>>();
const grantMenuRef = ref<InstanceType<typeof GrantMenu>>();
const state = reactive({
	loading: false, 
	roleData: [] as Array<any>,
	queryParams: {
		tenantId: undefined,
		name: undefined,
		code: undefined,
	},
	tableParams: {
		pageNo: 1,
		pageSize: 50,
		total: 0 as any,
	},
	editRoleTitle: '',
});

onMounted(async () => {
	await handleQuery();
});

// 查询操作
const handleQuery = async () => {
	state.loading = true;
	let params = Object.assign(state.queryParams, state.tableParams);
	let res = await apiSysRolePagePost(params);
	state.roleData = res.data.data?.rows ?? [];
	state.tableParams.total = res.data.data?.totalRows;
	state.loading = false;
};

// 重置操作
const resetQuery = async () => {
	state.queryParams.name = undefined;
	state.queryParams.code = undefined;
	await handleQuery();
};

// 打开新增页面
const openAddRole = () => {
	state.editRoleTitle = '添加角色';
	editRoleRef.value?.openDialog({ id: undefined, isDisable: 0,  sort: 100 });
};

// 打开编辑页面
const openEditRole = async (row: any) => {
	state.editRoleTitle = '编辑角色';
	editRoleRef.value?.openDialog(row);
};

// 打开授权数据范围页面
const openGrantData = (row: any) => {
	grantDataRef.value?.openDialog(row);
};

//分配菜单
const openGrantMenu = (row: any) => {
	state.editRoleTitle = $t('system.role.grantMenu') + ":[ "+row.name+" ]";
	grantMenuRef.value?.openDialog(row);
};

// 删除
const delRole = (row: any) => {
	ElMessageBox.confirm($t('system.btn.deleteConfirm') + `：【${row.name}】?`, $t('system.btn.confirm'), {
		confirmButtonText: $t('system.btn.confirm'),
		cancelButtonText: $t('system.btn.undo'),
		type: 'warning',
	})
		.then(async () => {
			await apiSysRoleDeletePost({ id: row.id });
			await handleQuery();
			ElMessage.success($t('system.btn.delete') + $t('system.btn.success'));
		})
		.catch(() => {});
};

// 改变页面容量
const handleSizeChange = async (val: number) => {
	state.tableParams.pageSize = val;
	await handleQuery();
};

// 改变页码序号
const handleCurrentChange = async (val: number) => {
	state.tableParams.pageNo = val;
	await handleQuery();
};

// 修改状态
const changeStatus = async (row: any) => {
	await apiSysRoleSetStatusPost({ id: row.id, isDisable: row.isDisable })
		.then(() => {
			ElMessage.success('角色状态设置成功');
		})
		.catch(() => {
			row.status = row.isDisable == 1 ? 0 : 1;
		});
};
</script>
